# -*- coding: utf-8 -*-
# authors 13077/13130
# data: 30 de Novembro de 2014
#
import time
import stats


class Controller:
    def __init__(self, data):
        self.data = data
        self.dic = {u"Índice de preços no consumidor": ['ipc', "Anos", "Percentagem"],
                    u"Variância Anual": ['va', "Anos", "Percentagem"],
                    u"Remuneração mínima mensal": ['rem_min', "Anos", "Euros"],
                    u"Remuneração máxima mensal": ['rem_max', "Anos", "Euros"],
                    u"PIB per capita": ['pib_per_cap', "Anos", "Euros"],
                    u"RNB per capita": ['rnb_per_cap_an', "Anos", "Euros"],
                    u"RDB per capita": ['rdb_per_cap_an', "Anos", "Euros"],
                    u"Remunerações per capita": ['rem_per_cap', "Anos", "Euros"]}

    def plot_from_attribute(self, attribute, start, end, reg, avg, med):
        """

        :param attribute:
        :param start:
        :param end:
        :param reg:
        :param avg:
        :param med:
        """
        t1 = time.time()
        if start > end:
            aux = end
            end = start
            start = aux
        items = self.dic.get(attribute)  # item[0] = attribute, item[1] = xtick label, item[2} = ytick label
        list_year = []
        list_value = []

        for x in self.data.query_interval(start, end):

            if x.get_from_attribute(items[0]) is not None:
                list_year.append(x.ano)
                list_value.append(x.get_from_attribute(items[0]))

        stats.plot_simple(attribute, items[1], items[2], list_year, list_value, reg, avg, med)
        #self.graphics.plot_simple(attribute, items[1], items[2], list_year, list_value, reg, avg, med)
        print "plot_from_attribute exec time: " + str(round(time.time() - t1, 3))
        pass

    def plot_correlation(self, attribute_1, attribute_2, start, end):
        #print attribute_1, attribute_2
        """

        :param attribute_1:
        :param attribute_2:
        :param start:
        :param end:
        """
        t1 = time.time()

        if start > end:
            aux = end
            end = start
            start = aux
        item_1 = self.dic.get(attribute_1)
        item_2 = self.dic.get(attribute_2)
        list_value_1 = []
        list_value_2 = []

        for x in self.data.query_interval(start, end):
            val_1 = x.get_from_attribute(item_1[0])
            val_2 = x.get_from_attribute(item_2[0])
            if val_1 is not None and val_2 is not None:
                list_value_1.append(val_1)
                list_value_2.append(val_2)

        stats.plot_correlation(list_value_1, list_value_2, start, end, attribute_1, attribute_2)

        print "plot_correlation exec time: " + str(round(time.time() - t1, 3))
        pass


